import { AxiosRequestConfig } from 'axios';
import http from '@/utils/http';

export async function getCategories(config?: AxiosRequestConfig<any>) {
    const _config = <AxiosRequestConfig<any>>{
        url: '/api/CarStyle/GetCarStyleType',
        method: 'POST',
        ...config,
    };
    const response = await http.request(_config);
    return response.data;
}

export async function downloadUrl(url: string, filename: string = 'download') {
    if (url === null) return;
    const response = await http.request({
        url,
        responseType: 'blob',
    });
    const blob = response.data;
    const downloadEl = document.createElement('a');
    const downloadUrl = window.URL.createObjectURL(blob);
    downloadEl.download = filename;
    downloadEl.href = downloadUrl;
    downloadEl.click();
    window.URL.revokeObjectURL(downloadUrl);
    downloadEl.remove();

    // fetch(url)
    //     .then(res => res.blob())
    //     .then(blob => {
    //         const downloadEl = document.createElement('a');
    //         const downloadUrl = window.URL.createObjectURL(blob);
    //         downloadEl.download = filename;
    //         downloadEl.href = downloadUrl;
    //         downloadEl.click();
    //         window.URL.revokeObjectURL(downloadUrl);
    //         downloadEl.remove();
    //     });
}
